package cn.enilu.flash.mobile.controller;

import cn.enilu.flash.bean.entity.blindbox.BlindBox;
import cn.enilu.flash.bean.entity.blindbox.BlindBoxGoods;
import cn.enilu.flash.bean.entity.blindbox.UserFunds;
import cn.enilu.flash.bean.entity.shop.Goods;
import cn.enilu.flash.bean.vo.blindbox.BlindBoxGoodsUserVO;
import cn.enilu.flash.bean.vo.blindbox.BlindBoxGoodsVO;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.blindbox.BlindboxGoodsService;
import cn.enilu.flash.service.blindbox.BlindboxGoodsUserService;
import cn.enilu.flash.service.blindbox.BlindboxService;
import cn.enilu.flash.service.blindbox.UserFundsService;
import cn.enilu.flash.service.shop.OrderService;
import cn.enilu.flash.utils.HttpUtil;
import cn.enilu.flash.web.controller.BaseController;
import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 盲盒
 *
 * @author HYF
 * @ClassName BlindBoxController
 * @date 2022年04月27日 5:15 下午
 */
@RestController
@RequestMapping("/blind-box")
public class BlindBoxController extends BaseController {

    @Autowired
    private BlindboxService blindboxService;
    @Autowired
    private BlindboxGoodsService blindboxGoodsService;
    @Autowired
    private BlindboxGoodsUserService blindboxGoodsUserService;
    @Autowired
    private UserFundsService userFundsService;

    /**
     * 盲盒列表
     * @return
     */
    @GetMapping("/blind-box-list")
    public Object blindBoxList() {
        List<BlindBox> blindBoxList = blindboxService.queryAll();
        return Rets.success(blindBoxList);
    }

    /**
     * 根据盲盒ID 查询商品列表
     * @param id
     * @return
     */
    @RequestMapping("/blind-box-goods-list/{id}")
    public Object blindBoxGoodsList(@PathVariable(name = "id")Long id) {
        List<BlindBoxGoodsVO> blindBoxList = blindboxGoodsService.queryBlindBoxGoodsList(id);
        return Rets.success(blindBoxList);
    }

    /**
     * 我的盲盒商品列表
     * @return
     */
    @RequestMapping("/blind-box-goods-user-list")
    public Object blindBoxGoodsUserList() {
        Long idUser = getIdUser(HttpUtil.getRequest());
        List<BlindBoxGoodsUserVO> blindBoxList = blindboxGoodsUserService.queryBlindBoxGoodsUserList(idUser);
        return Rets.success(blindBoxList);
    }


    /**
     * 盲盒抽奖
     * @param orderSn 订单编号
     * @return
     */
    @RequestMapping("/luck-draw/{orderSn}")
    public Object luckDraw(@PathVariable(name = "orderSn")String orderSn) {
        Long idUser = getIdUser(HttpUtil.getRequest());
        List<Goods> blindBoxList = blindboxService.luckDraw(idUser,orderSn);
        return Rets.success(blindBoxList);
    }

    /**
     * 回收商品
     * @param ids
     * @return
     */
    @RequestMapping(value = "/recovery-goods",method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public synchronized Object recoveryGoods(@RequestBody List<Long> ids){
        Long idUser = getIdUser(HttpUtil.getRequest());
        Long price =  blindboxGoodsUserService.recoveryGoods(ids,idUser);
        UserFunds userFunds = userFundsService.findUserFundsByUserId(idUser);
        userFunds.setBalance(userFunds.getBalance()+price);
        return Rets.success();
    }

}